package com.taotao.search.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.parsing.QualifierEntry;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.pojo.SearchResult;
import com.taotao.search.service.QueryItemService;

@Controller
public class QueryItems {
	@Autowired
	private QueryItemService queryService;
	
	@Value("${SEARCH_ROWS}")
	private int SEARCH_ROWS;
	@RequestMapping("/search")
	public String search(@RequestParam("q")String queryString,@RequestParam(defaultValue="1")int page,Model model) throws Exception{
		queryString = new String(queryString.getBytes("iso-8859-1"),"utf-8");
		SearchResult queryItem = queryService.queryItem(queryString, page, SEARCH_ROWS);
		System.out.println(queryItem.getTotalCount());
		model.addAttribute("query", queryString);
		model.addAttribute("totalPages", queryItem.getTotalPage());
		model.addAttribute("itemList", queryItem.getList());
		model.addAttribute("page", page);
		return "search";
	}
}
